﻿import QtQuick
import QtQuick.Controls
import FilUI

Image {
    id: control

    property string errorButtonText: qsTr("Reload")
    property var clickErrorListener : function() {
        image.source = ""
        image.source = control.source
    }
    property Component errorItem : com_error
    property Component loadingItem: com_loading

    Loader {
        anchors.fill: parent
        sourceComponent: {
            if (control.status === Image.Loading) {
                return com_loading
            } else if (control.status == Image.Error) {
                return com_error
            } else {
                return undefined
            }
        }
        Component.onDestruction: sourceComponent = undefined
    }
    Component {
        id: com_loading
        Rectangle {
            color: FilTheme.itemHoverColor
            FilProgressRing {
                anchors.centerIn: parent
                visible: control.status === Image.Loading
            }
        }
    }
    Component {
        id: com_error
        Rectangle {
            color: FilTheme.itemHoverColor
            FilFilledButton {
                text: control.errorButtonText
                anchors.centerIn: parent
                visible: control.status === Image.Error
                onClicked: clickErrorListener()
            }
        }
    }
}
